package com.EM1.www.servlet;

import com.EM1.www.dao.ConnectExam;
import com.EM1.www.po.Paper;
import com.EM1.www.po.Teacher;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/Release")
public class Release extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String startdate = request.getParameter("startdate");
        String starttime=request.getParameter("starttime");
        String enddate=request.getParameter("enddate");
        String endtime=request.getParameter("endtime");
        String grade=request.getParameter("grade");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yy-MM-ddhh:mm:ss");
        try {
            Date start = simpleDateFormat.parse(startdate + starttime);
            Date end=simpleDateFormat.parse(enddate+endtime);
            ConnectExam connectExam=new ConnectExam();
            HttpSession session = request.getSession();
            Paper paper= (Paper)session.getAttribute("paper");
            Teacher teacher = (Teacher) session.getAttribute("teacher");
            connectExam.insertExam(paper.getId(),start,end,teacher.getId(),Integer.parseInt(grade));
            request.getRequestDispatcher("paper.jsp");
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
